ledstrip={} 
local sbuf = ws2812.newBuffer(12,3)
local btmr = tmr.create()
ledstrip.bcnt = 0
function ledstrip.init(replace)
    ws2812.init(ws2812.MODE_SINGLE)
    if replace==nil then   
        sbuf:fill(0,0,0)
        ws2812.write(sbuf)
    end
end
function ledstrip.welcome(tm)
  if tm==nil then tm= 800 end
  ws2812_effects.init(sbuf)
  ws2812_effects.set_brightness(20)
  ws2812_effects.set_speed(10) 
  ws2812_effects.set_delay(100)
  ws2812_effects.set_mode("rainbow_cycle", 1)
  ws2812_effects.start()  
  btmr:register(tm, tmr.ALARM_AUTO, function(t)
    ws2812_effects.stop()
    ledstrip.off()
    t:unregister()
    t:stop()
    ledstrip.bcnt =0
  end)
  ledstrip.bcnt = 1
  btmr:start()  
end
function ledstrip.waiting(cmd)    
    if(cmd==1)then    
        ws2812_effects.init(sbuf)
        ws2812_effects.set_mode("gradient", string.char(16,0,0,16,16,0,0,16,0))             
        btmr:register(200, tmr.ALARM_AUTO, function()            
            ledstrip.shift(1)
        end)
        ledstrip.bcnt = 1
        btmr:start()
    else
        ledstrip.bcnt = 0
        btmr:stop()         
        ledstrip.off()
    end    
end
function ledstrip.lowBat(c)  
  if(c==1)then
    ledstrip.blink(0,330,3,3,16,0,0,0,0, function()
        ledstrip.off()
    end)
  elseif (c==2) then -- very low battery
    ledstrip.blink(0,330,3,0,15,0,0,0,0, function()
        ledstrip.off()
    end)
  else--normal battery
    ledstrip.blink(0,330,2,15,0,0,0,0,0, function()
        ledstrip.off()
    end)
  end
end
function ledstrip.charging(cmd,per)--per from 1 to 12
    if(cmd==1)then
        ledstrip.off()
        ledstrip.lights(0,per,16,0,0)                    
        btmr:register(300, tmr.ALARM_AUTO, function()            
            ledstrip.bcnt=ledstrip.bcnt+1
            if(ledstrip.bcnt%10>2)then
                ledstrip.light(per,0,0,0,true)
            else
                ledstrip.light(per,16,16,0,true)
            end
        end)
        ledstrip.bcnt = 1
        btmr:start()
    else
        ledstrip.bcnt = 0
        btmr:stop()         
        ledstrip.off()
    end    
end
function ledstrip.start(ang,stp)
    if(ang>=12)then ang=12 end
    if(ang<=0)then ang=12 end
    if(stp)then        
        local idx = ang
        local cnt = stp        
        if(idx>=0 and idx<=12 and cnt>0 and sbuf~=0)then
            --if(idx==12) then idx=0 end
            if(idx==12) then idx = -1 else idx= idx-1 end
            sbuf:fill(0,0,0)
            for tmp=1,cnt do
                if tmp==cnt then sbuf:set(tmp,16,16,0) else sbuf:set(tmp,16,0,0) end
            end
            ledstrip.shift(idx)       
            ws2812.write(sbuf)
        end
    else
        ledstrip.blink(ang,200,3,0,16,0,0,0,0,function()
            ledstrip.light(ang,16,0,0)
        end)
    end
end
function ledstrip.over(ang)
    if(ang>=12)then ang=12 end
    if(ang<=0)then ang=12 end     
    ledstrip.light(ang,0,16,0)
end
function ledstrip.on(R,G,B)
    if(sbuf~=0)then 
        sbuf:fill(R,G,B)
        ws2812.write(sbuf)
    end
end
function ledstrip.off()
    if(sbuf~=0)then 
        sbuf:fill(0,0,0)
        ws2812.write(sbuf)
    end
end
function ledstrip.light(idx,G,R,B,replace)
    if(idx>=0 and idx<=12 and sbuf~=0)then--idx 0~12
        --if(idx==12)then idx=1 else idx=idx+1 end
        if(idx==0)then idx= 1 end
        if(replace==nil)then sbuf:fill(0,0,0) end
        sbuf:set(idx, G,R,B)    
        ws2812.write(sbuf)
    end    
end
function ledstrip.lights(idx,cnt,G,R,B)
    if(idx>=0 and idx<=12 and cnt>0 and sbuf~=0)then
        --if(idx==12) then idx=0 end
        if(idx==0)then idx= 1 end
        sbuf:fill(0,0,0)
        for tmp=1,cnt do sbuf:set(tmp,G,R,B) end
        ledstrip.shift(idx)
        ws2812.write(sbuf)
    end
end

function ledstrip.blink(idx,ms,cnt,G,R,B,G1,R1,B1,fun)
    if(cnt==0 or ms==0 or idx<0 or idx>12 or sbuf==0)then
        btmr:stop()
        return
    end
    if btmr then
        btmr:unregister()
        --if(idx==0)then
        --    idx = -1
        --end
        --if(idx==12)then idx=1 else idx=idx+1 end
        
        btmr:register(ms, tmr.ALARM_AUTO, function()
            ledstrip.bcnt= ledstrip.bcnt+1
            if(ledstrip.bcnt/2 > cnt)then
                if(idx==0) then 
                    sbuf:fill(0,0,0)
                    ws2812.write(sbuf)
                end
                ledstrip.bcnt = 0
                btmr:stop()
                if(fun)then
                    fun()
                end
            end        
            if(ledstrip.bcnt%2==0)then
                if(idx==0)then sbuf:fill(G,R,B)  else     sbuf:set(idx,G,R,B)  end
            else
                if(idx==0)then sbuf:fill(G1,R1,B1)  else     sbuf:set(idx,G1,R1,B1)  end
            end
            ws2812.write(sbuf)            
            
        end)
        
    end
    ledstrip.bcnt = 1
    if(idx==0)then  sbuf:fill(G,R,B)  else     sbuf:set(idx,G,R,B)  end
    btmr:start()
end
function ledstrip.shift(stp)
    sbuf:shift(stp,ws2812.SHIFT_CIRCULAR,1,-1)
    ws2812.write(sbuf)
end
return ledstrip
